package com.gadgets.framework.sysParam.controller;

import com.gadgets.framework.common.dto.HttpBaseResp;
import com.gadgets.framework.common.dto.Pagination;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import com.gadgets.framework.common.util.CacheUtils;
import com.gadgets.framework.sysParam.dto.SysParamQueryDTO;
import com.gadgets.framework.sysParam.dto.SysParamQueryResultDTO;
import com.gadgets.framework.sysParam.dto.SysParamUpdateDTO;
import com.gadgets.framework.sysParam.service.ISysParamManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2021-07-19
 * @moduel gadgets
 * 
 */
@RestController
@RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/sysParam")
public class SysParamManageController {

    @Autowired
    private CacheUtils cacheUtils;

    @Autowired
    @Qualifier(value = "sysParammanageService")
    private ISysParamManageService service;

    /**
    * 分页条件查询数据库系统参数信息
    * @param request       请求信息
    * @param response      应答信息
    * @param queryDTO      查询条件
    * @param pagination    分页条件
    * @return              查询结果
    * @throws GadgetsServiceException  异常信息
    */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/queryParamCode.do")
    //@RoleAuthCheck
    public HttpBaseResp<SysParamQueryResultDTO> querySysParamByCondition(HttpServletRequest request, HttpServletResponse response, SysParamQueryDTO queryDTO, Pagination pagination) throws GadgetsServiceException {
        List<SysParamQueryResultDTO> list = service.selectSysParamByConditionAndPagination(queryDTO,pagination);
        return new HttpBaseResp<>(list,pagination);
    }


    /**
     * 更新系统参数信息
     * @param request                   请求信息
     * @param response                  应答信息
     * @param updateDTO                 参数更新信息
     * @return                          通用应答
     * @throws GadgetsServiceException  异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/updateSysParam.do")
    //@RoleAuthCheck
    @SuppressWarnings("rawtypes")
    public HttpBaseResp updateSysParamValue(HttpServletRequest request, HttpServletResponse response, SysParamUpdateDTO updateDTO) throws GadgetsServiceException{
        service.updateSysParamValue(updateDTO);
        cacheUtils.putSysParamCache(updateDTO.getParamCode(), updateDTO.getParamValue());
        return new HttpBaseResp();
    }

    /**
     * 系统参数缓存刷新
     * @return                          刷新结果
     * @throws GadgetsServiceException  异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/resetParamCache.do")
    //@RoleAuthCheck
    public HttpBaseResp resetParamValue() throws GadgetsServiceException{
        return new HttpBaseResp();
    }

}
